package com.chengyu.stock.mq;

import com.chengyu.stock.pojo.domain.InnerMarketDomain;
import com.chengyu.stock.service.StockService;
import com.chengyu.stock.vo.resp.R;
import com.github.benmanes.caffeine.cache.Cache;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.joda.time.Period;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

//监听股票相关消息的消费者bean
@Component
@Slf4j
public class ReceiveStockListener {
    @Autowired
    private Cache<String,Object> caffeineCache;
    @Autowired
    private StockService stockService;

    @RabbitListener(queues = "innerMarketQueue")
    public void receiveInnerMarketMsg(Date date) throws Exception{
        //统计发送消息和消费消息的时间间隔
        int diffTime = new Period(DateTime.now(), new DateTime(date)).getMillis();
        log.info("消息传递耗费时间：{}",diffTime);
        //调用服务器获取缓存数据，一分钟只调用一次这个服务即可
        R<List<InnerMarketDomain>> infos = stockService.getNewestInnerMarketInfos();
        //刷新缓存
        caffeineCache.put("innerMarketInfosKey",infos);
    }
}
